const Sequelize = require('sequelize');
const config = require('../config');
const db = config.db;

const sequelize = new Sequelize(db.database, db.username, db.password, {
  host: db.host,
  dialect: 'mysql',
  pool: {
    max: 5,
    min: 0,
    idle: 10000,
  },
});

function defineModel(name, attrs, ...cfg) {
  let ocfg = {};
  if (cfg.length > 0) {
    ocfg = cfg[0];
  }
  ocfg.freezeTableName = true;
  return sequelize.define(name, attrs, ocfg);
}

const TYPES = ['STRING', 'INTEGER', 'BIGINT', 'TEXT', 'DOUBLE', 'DATEONLY', 'BOOLEAN', 'DATE', 'FLOAT'];
const exp = {
  define: defineModel,
  sync: () => {
    sequelize.sync({ force: true });
  },
  query: (sql) => {
    return sequelize.query(sql);
  },
};
for (const i in TYPES) {
  exp[TYPES[i]] = Sequelize[TYPES[i]];
}
module.exports = exp;

